//
//  JWMyCollectionListVC.m
//  jinwang
//
//  Created by jzb on 2018/4/4.
//  Copyright © 2018年 jzb. All rights reserved.
//

#import "JWMyCollectionListVC.h"
#import "JWMyCollectionListShotCell.h"
#import "JWMyCollectionListLoveshotCell.h"
#import "JWMyCollectionListGoodArticleCell.h"
#import "BJDynamicSnapshotModel.h"
#import "JWAlertChoseButsView.h"
#import "ReleaseBoJianVC.h"
#import "JWBoJianDetailsVC.h"

@interface JWMyCollectionListVC ()

@property (nonatomic,strong) JWMyCollectionListShotCell *SnapshotCell;
@property (nonatomic,strong) JWMyCollectionListLoveshotCell *LoveshotCell;
@property (nonatomic,strong) JWMyCollectionListGoodArticleCell *GoodArticleCell;
@property (nonatomic,strong) BJDynamicSnapshotModel *Listmodel,*Exitmodel;
@property (nonatomic,assign) int page;

@property (nonatomic,strong) NSMutableArray *ListmodelArr;

@end

@implementation JWMyCollectionListVC

- (void)viewDidLoad {
    [super viewDidLoad];
    _page = 1;
    [self setUpTableView];
    [self makeUrl_user_collectionList];
}
//配置tableView
-(void)setUpTableView{
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.tableView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    //注册cell
    self.tableView.estimatedRowHeight = UITableViewAutomaticDimension;
    [self registerCellWithCellClassArray:@[NSStringFromClass([JWMyCollectionListShotCell class])]];
    [self registerCellWithCellClassArray:@[NSStringFromClass([JWMyCollectionListLoveshotCell class])]];
    [self registerCellWithCellClassArray:@[NSStringFromClass([JWMyCollectionListGoodArticleCell class])]];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        [self makeUrl_user_collectionList];
    }];
    //上啦加载更多
    self.tableView.mj_footer  = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        _page++;
        [self makeUrl_user_collectionList];
    }];
}
#pragma mark -UITableViewDelegate,UITableViewDataSource
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _ListmodelArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BJDynamicSnapshotModel *model = _ListmodelArr[indexPath.row];
    
    if ([model.post_type integerValue] == 0) {
        
        self.SnapshotCell = [tableView dequeueReusableCellWithIdentifier:[JWMyCollectionListShotCell getCellReuseIdentifier]];
        self.SnapshotCell.contentView.backgroundColor = [UIColor clearColor];
        
        [self.SnapshotCell.upDownBtn addTarget:self action:@selector(cellBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        self.SnapshotCell.upDownBtn.tag = 1000+indexPath.row;
        
        self.SnapshotCell.model = self.ListmodelArr[indexPath.row];
        return self.SnapshotCell;
        
    }else if ([model.post_type integerValue] == 1){
        self.LoveshotCell = [tableView dequeueReusableCellWithIdentifier:[JWMyCollectionListLoveshotCell getCellReuseIdentifier]];
        self.LoveshotCell.contentView.backgroundColor = [UIColor clearColor];
        
        [self.LoveshotCell.upDownBtn addTarget:self action:@selector(cellBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        self.LoveshotCell.upDownBtn.tag = 1000+indexPath.row;
        
        self.LoveshotCell.model = self.ListmodelArr[indexPath.row];
        return self.LoveshotCell;
    }else{
        self.GoodArticleCell = [tableView dequeueReusableCellWithIdentifier:[JWMyCollectionListGoodArticleCell getCellReuseIdentifier]];
        self.GoodArticleCell.contentView.backgroundColor = [UIColor clearColor];
        
        [self.GoodArticleCell.upDownBtn addTarget:self action:@selector(cellBtnOnclick:) forControlEvents:UIControlEventTouchUpInside];
        self.GoodArticleCell.upDownBtn.tag = 1000+indexPath.row;
        
        BJDynamicSnapshotModel *model = self.ListmodelArr[indexPath.row];
        self.GoodArticleCell.model = model;
        
        for (UIView *view in self.GoodArticleCell.imgsBGView.subviews) {
            
            [view removeFromSuperview];
        }
        if (model.imgs.count > 0) {
            
            self.GoodArticleCell.imgsBGView.imageUrls = model.imgs;
        }
        
        return self.GoodArticleCell;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    BJDynamicSnapshotModel *model = _ListmodelArr[indexPath.row];
    
    if ([model.post_type integerValue] == 0 ||[model.post_type integerValue] == 1 ) {
        
        return 190;
        
    }else{
        if (model.imgs.count > 0) {
            
            return 125-17 + [self cellContentViewWith:self.GoodArticleCell.imgsBGView.width withModel:model];
        }else{
            return 125;
        }
    }
}
- (CGFloat)cellContentViewWith:(CGFloat)width withModel:(BJDynamicSnapshotModel *)model
{
    CGFloat Height = (width - 20)/3 + 5;
    CGFloat CellHeight = 0;
    
    CGSize size = CGSizeMake(kScreenWidth - 30, MAXFLOAT);//设置高度宽度的最大限度
    CGRect rect = [model.post_title boundingRectWithSize:size options:NSStringDrawingUsesFontLeading|NSStringDrawingTruncatesLastVisibleLine|NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil];
    
    if (model.imgs.count % 3 > 0) {
        
        CellHeight = Height*(model.imgs.count/3+1);
        
    }else{
        
        CellHeight = Height*(model.imgs.count/3);
    }
    
    return CellHeight + rect.size.height;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    JWBoJianDetailsVC *vc = [JWBoJianDetailsVC new];
    BJDynamicSnapshotModel *model = self.ListmodelArr[indexPath.row];
    vc.post_id = [NSString stringWithFormat:@"%@",model.post_id];
    [self.navigationController pushViewController:vc animated:YES];
}
- (void)cellBtnOnclick:(UIButton *)sender{
    
    _Exitmodel = self.ListmodelArr[sender.tag - 1000];
    
    UITableViewCell *cell = (UITableViewCell *)[[[sender superview] superview] superview];
    
    NSIndexPath *indexPath = [self.tableView indexPathForCell:cell];
    
    [self makeUrl_user_delCollectionIndexPath:indexPath];
    
}

#pragma mark -网络请求
//我的收藏列表
-(void)makeUrl_user_collectionList{
    [[DDNetworkManagerDate makeUrl_user_collectionListWithtoken:k_user_token page:[NSString stringWithFormat:@"%d",_page]] post_RequestFinshSuccess:^(id responseObject) {
        NSLog(@"-=-=>>%@",responseObject);
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            [self.tableView.mj_header endRefreshing];
            [self.tableView.mj_footer endRefreshing];
            //            _ListmodelArr = [NSMutableArray new];
            NSArray *data = [responseObject objectForKey:kRequsetDataKey];
            //转模型
            NSMutableArray *arr = [NSMutableArray array];
            [data enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                BJDynamicSnapshotModel *model = [BJDynamicSnapshotModel yy_modelWithDictionary:obj];
                [arr addObject:model];
            }];
            //            self.ListmodelArr = arr.mutableCopy;
            if (_page == 1) {
                self.ListmodelArr = arr.mutableCopy;
            }else{
                [self.ListmodelArr addObjectsFromArray:arr];
            }
        }
        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        [self showBottomMessag:@"网络请求失败"];
    }];
}
//删除收藏
-(void)makeUrl_user_delCollectionIndexPath:(NSIndexPath *)indexPath{
    [[DDNetworkManagerDate makeUrl_user_delCollectionWithtoken:k_user_token collection_id:[_Exitmodel.collection_id stringValue]] post_RequestFinshSuccess:^(id responseObject) {
        
        if ([self requestFinshCode:responseObject isShowMessage:YES] == 1) {
            
            [_ListmodelArr removeObjectAtIndex:indexPath.row];
            [self.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationAutomatic];
        }
//        [self.tableView reloadData];
    } failure:^(id errorObject) {
        [self showBottomMessag:@"网络请求失败"];
    }];
}

-(NSMutableArray *)ListmodelArr{
    if (!_ListmodelArr) {
        _ListmodelArr = [NSMutableArray array];
    }
    return _ListmodelArr;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
